import 'package:backend.core/core/domain/entities/web/web_subscription.dart';
import 'package:equatable/equatable.dart';
import 'package:backend.purchases/infrastructure/dtos/api/api_money_amount_dto.dart';
import 'package:backend.purchases/infrastructure/dtos/api/web/api_web_purchase_item_dto.dart';
import 'package:backend.purchases/infrastructure/dtos/api/web/api_web_subscription_duration_dto.dart';

const kApiWebSubscriptionDurationKey = 'duration';

const kApiWebSubscriptionTypeValue = 'subscription';

class ApiWebSubscriptionDto extends Equatable implements ApiWebPurchaseItemDto {
  const ApiWebSubscriptionDto._internal({
    required this.sku,
    required this.priceDto,
    required this.title,
    required this.legacy,
    required this.durationDto,
  });

  @override
  final String sku;

  @override
  final ApiMoneyAmountDto priceDto;

  @override
  final String title;

  @override
  final bool legacy;

  final ApiWebSubscriptionDurationDto durationDto;

  @override
  List<Object?> get props {
    return [
      sku,
      priceDto,
      title,
      legacy,
      durationDto,
    ];
  }

  @override
  String get type => kApiWebSubscriptionTypeValue;

  factory ApiWebSubscriptionDto.fromEntity(WebSubscription subscription) {
    final priceDto = ApiMoneyAmountDto.fromMoneyAmount(subscription.price);
    final durationDto = ApiWebSubscriptionDurationDto.fromEntity(subscription.duration);

    return ApiWebSubscriptionDto._internal(
      sku: subscription.sku,
      priceDto: priceDto,
      title: subscription.title,
      legacy: subscription.legacy,
      durationDto: durationDto,
    );
  }

  @override
  Map<String, dynamic> toJson() {
    return {
      kApiWebPurchaseItemTypeKey: type,
      kApiWebPurchaseItemSkuKey: sku,
      kApiWebPurchaseItemPriceKey: priceDto.toJson(),
      kApiWebPurchaseItemTitleKey: title,
      kApiWebPurchaseItemLegacyKey: legacy,
      kApiWebSubscriptionDurationKey: durationDto.toString(),
    };
  }
}
